package com.pengcheng.taskapplication.mvp.view.fragment;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.pengcheng.taskapplication.R;
import com.pengcheng.taskapplication.contants.ConstantValue;
import com.pengcheng.taskapplication.helper.ImageLoader;
import com.pengcheng.taskapplication.model.UserDetailVo;
import com.pengcheng.taskapplication.mvp.presenter.UserPresenter;
import com.pengcheng.taskapplication.mvp.view.activity.ApplyListActivity;
import com.pengcheng.taskapplication.mvp.view.activity.Html5AgressActivity;
import com.pengcheng.taskapplication.mvp.view.activity.MainActivity;
import com.pengcheng.taskapplication.mvp.view.activity.MyApplyAndCarsList;
import com.pengcheng.taskapplication.mvp.view.activity.MyCarListSign;
import com.pengcheng.taskapplication.mvp.view.activity.MyParkAndWashList;
import com.pengcheng.taskapplication.mvp.view.activity.MyWalletBankList;
import com.pengcheng.taskapplication.mvp.view.activity.PaySettingActivity;
import com.pengcheng.taskapplication.mvp.view.activity.UploadIdCard;
import com.pengcheng.taskapplication.mvp.view.activity.UserInfoActivity;
import com.pengcheng.taskapplication.mvp.view.widget.CircleImageView;
import com.pengcheng.taskapplication.net.IView;
import com.pengcheng.taskapplication.rxbus.RxBus;
import com.pengcheng.taskapplication.rxbus.RxBusRoute;
import com.pengcheng.taskapplication.rxbus.RxEvent;
import com.pengcheng.taskapplication.util.SPUtils;

import java.time.temporal.Temporal;

import butterknife.BindView;
import butterknife.OnClick;

public class MyInfoFragment  extends BaseFragment implements IView {

    @BindView(R.id.img_left)
    CircleImageView img_attr;
    @BindView(R.id.tv_money)
    TextView tv_userMoney;
    @BindView(R.id.tv_coupon)
    TextView tv_coupon;

    @BindView(R.id.tv_nick)
    TextView tv_nick;

    private UserPresenter userPresenter;
    @Override
    protected int layoutResID() {
        return R.layout.fragment_myinfo;
    }

    @Override
    protected void initData() {
        userPresenter=new UserPresenter(this);
        getUserDetail();
    }

    @Override
    public void onSuccess(String code, String msg, Object data, String actionType) {
        if (actionType.equals(ConstantValue.RequestKey.userinfo)) {
            UserDetailVo userDetailVo = (UserDetailVo) data;
            if (userDetailVo != null) {
                tv_nick.setText(userDetailVo.getCellphone()==null?"车智行":userDetailVo.getCellphone());
                SPUtils.putBoolean(context,ConstantValue.SpType.isreal,userDetailVo.isHasRealNameAuth());
                if(userDetailVo.getAvatar()==null||userDetailVo.getAvatar().length()==0){
                    ImageLoader.displayImage(context,R.mipmap.logo, img_attr);
                }else {
                    ImageLoader.displayImage(context, userDetailVo.getAvatar(), img_attr);
                }
            }
        }
    }

    @Override
    public void onFailed(String code, String msg) {

    }

    public void getUserDetail() {
        userPresenter.getUserDetail(ConstantValue.RequestKey.userinfo);
    }

    @OnClick({R.id.ll_dianzi_banli,R.id.tv_nick,R.id.ll_wash_order, R.id.ll_park_order, R.id.img_setting, R.id.rl_my_apply,R.id.rl_my_car_list,R.id.rl_my_money})
    public void onViewClicked(View view) {
        Intent intent;
        switch (view.getId()) {
            //洗车订单
            case R.id.ll_wash_order:
//                intent = new Intent(context, MyParkAndWashList.class);
//                startActivity(intent);
               // ((MainActivity)getActivity()).goRecommend(2);
                RxBus.getInstance().post(new RxEvent(RxBusRoute.Wash));
                break;
            case R.id.ll_dianzi_banli:
                boolean isreal=SPUtils.getBoolean(context, ConstantValue.SpType.isreal);
                if(isreal) {
                    intent = new Intent(context, Html5AgressActivity.class);
                    intent.putExtra("url", "file:///android_asset/instructions.html");
                    intent.putExtra("title", "办理须知");
                    startActivity(intent);
                }else{
                    intent = new Intent(context, UploadIdCard.class);
                    startActivity(intent);
                }
                break;
             //停车订单
            case R.id.ll_park_order:
//                intent = new Intent(context, MyApplyAndCarsList.class);
//                startActivity(intent);
                RxBus.getInstance().post(new RxEvent(RxBusRoute.Park));
                break;
             //我的申请
            case R.id.rl_my_apply:
                intent= new Intent(context, ApplyListActivity.class);
                startActivity(intent);
                break;
             //我的车辆
            case R.id.rl_my_car_list:
                intent =new Intent(context, MyCarListSign.class);
                startActivity(intent);
                break;
            //我的钱包
            case R.id.rl_my_money:
                intent =new Intent(context, MyWalletBankList.class);
                startActivity(intent);
                break;
            //设置
            case R.id.img_setting:
                intent = new Intent(context, UserInfoActivity.class);
                startActivity(intent);
                break;

            //设置
            case R.id.tv_nick:
                intent = new Intent(context, UserInfoActivity.class);
                startActivity(intent);
                break;


        }
    }

    @Override
    public void onResume() {
        super.onResume();
        getUserDetail();
    }
}
